package com.example.fill;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

public class Person  {

    private MoveState moveState = MoveState.STAND;
    private Bitmap[] stand = new Bitmap[2];
    private Bitmap[] right = new Bitmap[1];
    private Bitmap[] left = new Bitmap[2];

    public Bitmap now;

    public PointF local = new PointF(0, 0);

    private int bitmapIndex = 0;

    public Person(Context context) {
        Matrix matrix = new Matrix();
        stand[0] = BitmapFactory.decodeResource(context.getResources(), R.mipmap.s1);
        if (stand[0].getWidth() > 200) {
            float i = 200f/ stand[0].getWidth() ;
            float i1 = 200f/  stand[0].getHeight() ;
            matrix.setScale(i,i1);
            stand[0] = Bitmap.createBitmap(stand[0],0,0,stand[0].getWidth(),stand[0].getHeight(),matrix,false);
        }
        stand[1] = BitmapFactory.decodeResource(context.getResources(), R.mipmap.s2);
        if (stand[1].getWidth() > 200) {
            float i = 200f/ stand[1].getWidth() ;
            float i1 = 200f/  stand[1].getHeight() ;
            matrix.setScale(i,i1);
            stand[1] = Bitmap.createBitmap(stand[1],0,0,stand[1].getWidth(),stand[1].getHeight(),matrix,false);
        }
        right[0] = BitmapFactory.decodeResource(context.getResources(), R.mipmap.r1);
        if (right[0].getWidth() > 200){
            float i = 200f/ right[0].getWidth() ;
            float i1 = 200f/  right[0].getHeight() ;
            matrix.setScale(i,i1);
            right[0] = Bitmap.createBitmap(right[0],0,0,right[0].getWidth(),right[0].getHeight(),matrix,false);
        }
        now = stand[0];
        handler.sendEmptyMessage(0);
    }

    public void setMoveState(MoveState moveState) {
        if (this.moveState == moveState) return;
        handler.removeCallbacksAndMessages(null);
        this.moveState = moveState;
        switch (moveState) {
            case STAND:
                Log.i("TAG", "run: STAND");
                handler.sendEmptyMessage(0);
                break;
            case LEFT:

                local.x -= 6;
                Log.i("TAG", "run: LEFT");
                break;
            case RIGHT:
                handler.sendEmptyMessage(10);
                handler.sendEmptyMessage(11);
                Log.i("TAG", "run: RIGHT");
                break;
            case UP:
                break;
            case DOWN:
                break;
        }
    }

    private final Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            bitmapIndex++;
            switch (msg.what){
                case 0:
                    int i =  bitmapIndex % stand.length;
                    now = stand[i];
                    sendEmptyMessageDelayed(0,300);
                    Log.i("TAG", "handleMessage: 0");
                    break;
                case 1:

                case 10:
                    local.x += 3;
                    sendEmptyMessageDelayed(10,10);
                    break;
                case 11:
                    int a =  bitmapIndex % right.length;
                    now = right[a];
                    sendEmptyMessageDelayed(11,300);
                    break;
            }
        }
    };

}
